from tornado import httpclient


def demo_httpclient():
    # "实用性并不大"
    client = httpclient.HTTPClient()
    result = client.fetch("http://www.baidu.com")
    print(result.body.decode("utf8"))


async def demo_async_httpclient():
    client = httpclient.AsyncHTTPClient()
    result = await client.fetch("http://www.baidu.com")
    print(result.body.decode("utf8"))


if __name__ == '__main__':
    import tornado

    io_loop = tornado.ioloop.IOLoop.current()

    # run_sync 方法可以在运行完某个协程之后停止事件循环
    io_loop.run_sync(demo_async_httpclient)

    import asyncio

    # 用python3自带的事件循环处理，会一直循环
    # asyncio.ensure_future(demo_async_httpclient())
    # asyncio.get_event_loop().run_forever()

    #asyncio.get_event_loop().run_until_complete(demo_async_httpclient())
